package com.wedimob.content;

import java.io.InputStream;
import java.net.URI;

import com.wedimob.bo.SerializableBO;
import com.wedimob.common.Size;

/**
 * Content container.
 * 
 * @author melouadi
 * 
 */
public class Content extends SerializableBO<Content> {

	static Content createValid(URI aUri, String aContentType, long aSize) {
		return new Content(aUri, aContentType, aSize);
	}

	static Content createNonValid(URI aUri, Throwable aE) {
		return new Content(aUri, aE);
	}

	/**
	 * Time of creation.
	 */
	private long creationTime;

	/**
	 * Source.
	 */
	private URI uri;

	/**
	 * Size in octet.
	 */
	private Size size;

	/**
	 * Type.
	 */
	private ContentType type;

	/**
	 * Extension.
	 */
	private String extension;

	/**
	 * Stream.
	 */
	private InputStream stream;

	/**
	 * Error.
	 */
	private Throwable e;

	/**
	 * Valid response constructor.
	 * 
	 * @param aUri
	 *            source
	 * @param aResponse
	 *            response
	 */
	private Content(final URI aUri, final String aContentType, final long aSize) {
		super();
		this.uri = aUri;
		this.size = new Size(aSize);
		this.extension = ContentType.extractExtension(aContentType);
		this.type = ContentType.fromExtension(extension);
		creationTime = System.currentTimeMillis();
	}

	/**
	 * Non valid response constructor.
	 * 
	 * @param aUri
	 *            source
	 * @param aE
	 *            error
	 */
	private Content(final URI aUri, final Throwable aE) {
		super();
		this.uri = aUri;
		this.e = aE;
		creationTime = System.currentTimeMillis();
	}

	/**
	 * Retrieve size.
	 * 
	 * @return size
	 */
	public Size getSize() {
		return size;
	}

	/**
	 * Retrieve error.
	 * 
	 * @return error
	 */
	public Throwable getError() {
		return e;
	}

	/**
	 * Retrieve source.
	 * 
	 * @return source
	 */
	public URI getUri() {
		return uri;
	}

	/**
	 * Retrieve type.
	 * 
	 * @return type
	 */
	public ContentType getType() {
		return type;
	}

	/**
	 * Retrieve creation time.
	 * 
	 * @return creation time
	 */
	public long getCreationTime() {
		return creationTime;
	}

	/**
	 * Retrieve extension.
	 * 
	 * @return extension
	 */
	public String getExtension() {
		return extension;
	}

	/**
	 * Retrieve stream to read
	 * 
	 * @return reader
	 */
	public InputStream getInputStream() {
		return stream;
	}

	/**
	 * Define reader
	 * 
	 * @param aStream
	 */
	public void setInputStream(final InputStream aStream) {
		stream = aStream;
	}

}